package com.cspg.design.request;


import com.cspg.design.bean.UserInfo;
import com.cspg.design.common.Constant;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.model.UserModel;
import com.cspg.design.presenter.LoginPresenter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Login {

    private RetrofitUtil retrofitUtil = new RetrofitUtil();
    private Call call;
    private LoginPresenter presenter;
    private static final String type = "android";

    public Login(LoginPresenter presenter) {
        this.presenter = presenter;
    }

    public void loginNoCode(  String username,   String pwd) throws Exception
    {
//        username = "jhjs2";
//        pwd = "123456";

        call = retrofitUtil.createReq(IService.class).login(username,pwd);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    presenter.getLoginFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }

                if (!response.isSuccessful()) {
                    presenter.getLoginFailure(Constant.HTTP_NO_NET_WORK);
                    return;
                }
                Object obj = response.body();
                if (obj == null)
                {
                    presenter.getLoginFailure(Constant.HTTP_LOGIN_FAILURE);
                    return;
                }
                UserModel userModel= (UserModel) response.body();
                if (userModel == null || userModel.getEmployeeData() == null || userModel.getEmployeeData().getEmployeeID() == null)
                {
                    presenter.getLoginFailure(Constant.HTTP_LOGIN_FAILURE);
                    return;
                }
                if (null == userModel) {
                    presenter.getLoginFailure(Constant.HTTP_LOGIN_FAILURE);
                    return;
                }
                else
                {
                    presenter.loginSuccess(userModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                System.out.println("登录失败");
                presenter.getLoginFailure("登录失败");
            }
        });
    }
    public RequestBody getRequestBody(HashMap<String, String> hashMap) {
        StringBuffer data = new StringBuffer();
        if (hashMap != null && hashMap.size() > 0) {
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                data.append(key).append("=").append(val).append("&");
            }
        }
        String jso = data.substring(0, data.length() - 1);
        RequestBody requestBody =
                RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),jso);

        return requestBody;
    }
}
